package com.example.retrofit;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class login extends AppCompatActivity {

    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonLogin;
    String username;
    String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextPassword = findViewById(R.id.editTextPassword);
        buttonLogin = findViewById(R.id.buttonLogin);

        // 设置登录按钮点击事件
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginUser();
            }
        });
    }

    private void loginUser() {
        // 获取输入的用户名和密码
        username = editTextUsername.getText().toString();
        password = editTextPassword.getText().toString();

        retorfitLogin();
    }

    private void retorfitLogin() {
        // 创建 LoginRequest 对象
        LoginRequest loginRequest = new LoginRequest(username, password);
        // 创建 Retrofit 实例
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        // 创建 ApiService 实例
        ApiService apiService = retrofit.create(ApiService.class);

        // 调用 postLogin 方法，传递 LoginRequest 对象
        apiService.postLogin(loginRequest).enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                // 处理响应
                System.out.println(response.body());
                if (response.isSuccessful()) {
                    GsonBean gsonBean = response.body();
                    if (gsonBean != null) {
                        // 将 GsonBean 对象转换为 JSON 字符串并打印
                        String jsonResult = gsonBean.toString();
                        System.out.println(jsonResult);

                        // 获取具体字段值的示例
                        String msg = gsonBean.msg;
                        int code = Integer.parseInt(gsonBean.code);  // 如果 code 是数字的话
                        String token = gsonBean.token;
                        getSharedPreferences("user",MODE_PRIVATE).edit().putString("token",response.body().token).commit();
                        String string = getSharedPreferences("user", MODE_PRIVATE).getString("token", "");
                        System.out.println("储存的token"+string);

// 输出具体字段值
                        System.out.println("Msg: " + msg);
                        System.out.println("Code: " + code);
                        System.out.println("Token: " + token);


                        // 其他处理逻辑...
                    } else {
                        Log.e("新闻信息", "响应数据为空");
                    }
                } else {
                    Log.e("新闻信息", "请求失败，错误码：" + response.code());
                }
            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {
                // 处理请求失败
                Log.e("新闻信息", "请求失败，错误信息：" + throwable.getMessage());
            }
        });
    }
}